<template>
  <div id="app">
    <div class="img-box img01">
      <img alt="Vue logo" src="./assets/images/img01.jpg" />
    </div>
    <div class="img-box img02">
      <img alt="Vue logo" src="./assets/images/img02.jpg" />
    </div>
    <div class="form-wrap">
      <p id="form-id"></p>
      <van-form validate-first @failed="onFailed" @submit="onSubmit">
        <!-- 通过 pattern 进行正则校验 -->
        <div class="form-box">
          <label>姓名*已加密<span>*</span></label>
        </div>
        <van-field
            v-model="name"
            id="name"
            name="name"
            placeholder="请输入"
            :rules="[{ required: true }, { validator: validator1, message: '请输入中文' }]"
          />
        <div class="form-box">
          <label>电话*已加密<span>*</span></label>
        </div>
        <van-field
          v-model="mobile"
          id="mobile"
          name="mobile"
          placeholder="请输入"
          :rules="[{ required: true }, { validator: validator2, message: '请输入有效的手机号码' }]"
        />
        <div class="form-box">
          <label>安装地区<span>*</span></label>
        </div>
        <van-field
          readonly
          clickable
          id="province"
          name="province"
          :value="province"
          placeholder="请选择地区"
          :rules="[{ required: true , message: ''}]"
          @click="showPicker = true"
        />
        <van-popup v-model="showPicker" round position="bottom">
          <van-picker
            show-toolbar
            :columns="columns"
            @cancel="showPicker = false"
            @confirm="onConfirm"
          />
        </van-popup>
        <div class="form-box">
          <label>详细地址：（街道*门牌号*小区）<span>*</span></label>
        </div>
        <van-field
          v-model="address"
          id="address"
          name="address"
          placeholder="请输入"
          :rules="[{ required: true, message: '此项不能为空，请输入' }]"
        />
        <van-checkbox v-model="checked" shape="square" icon-size="14px" class="agree-rule">自动输入历史手机号<span @click="infoSafe">《个人信息授权与保护声明》</span></van-checkbox>
        <p class="tips">预约成功后，请注意接听0591-88710000来电！</p>
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit" class="submit-btn">立即预约 上门安装</van-button>
        </div>
      </van-form>
    </div>
    <div class="img-box img03">
      <img alt="Vue logo" src="./assets/images/img03.jpg" />
    </div>
    <div class="img-box img04">
      <img alt="Vue logo" src="./assets/images/img04.jpg" />
    </div>
     <!-- 立即预约 -->
    <div class="now-ok">
      <van-button type="info" @click="quickOrder"><van-icon name="records" />立即预约</van-button>
      <!-- <div class="tips">提交即视为您已阅读并同意<span @click="personInfo">《个人信息保护声明》</span></div> -->
    </div>
  </div>
</template>

<script>
import { Dialog } from 'vant';
export default {
  name: "App",
  components: {
    [Dialog.Component.name]: Dialog.Component,
  },
  data(){
    return {
      name: '',
      mobile: '',
      province: '',
      address: '',
      // pattern: /^[\u4e00-\u9fa5]{0,}$/,
      checked: false,
      showPicker: false,
      columns: ['福州', '厦门', '泉州', '漳州', '三明', '南平', '宁德', '莆田', '龙岩'],
    }
  },
  methods: {
    validator1(val) {
      if (/^[\u4e00-\u9fa5]{0,}$/.test(val)) {
        return true
      } else {
        location.href = "#name"
        return false
      }
    },
    // 校验函数返回 true 表示校验通过，false 表示不通过
    validator2(val) {
      if (/^1[3456789]\d{9}$/.test(val)) {
        return true
      } else {
        location.href = "#mobile"
        return false
      }
    },
    onFailed(errorInfo) {
      // console.log('failed', errorInfo);
      location.href = "#"+errorInfo.errors[0].name
    },
    onConfirm(value) {
      this.province = value;
      this.showPicker = false;
    },
    infoSafe() {
      Dialog.alert({
        title: '个人信息授权与保护声明',
        message: '信息保护内容',
      }).then(() => {
        // on close
      });
    },
    personInfo() {
      Dialog.alert({
        title: '个人信息授权与保护声明',
        message: '信息保护内容',
        confirmButtonText: '我已阅读'
      }).then(() => {
        // on close
      });
    },
    onSubmit(values) {
      let addVals = {
        price: '',
        projectName: '',
        channel: '',
        orderSourceUrl: '',
        category: ''
      }
      let data = Object.assign(values,addVals)
      // console.log(data)
      Dialog.alert({
        title: '',
        message: '表单提交，立即预约',
      }).then(() => {
        // on close
      });
    },
    quickOrder() {
      location.href = "#form-id"
    }
  },
};
</script>

<style scoped lang="less">
#app {
  font-family: '微软雅黑';
}
.van-cell::after {
  border-bottom-color: #c8c9cc;
}
.form-wrap {
  position: relative;
  margin-top: 20px;
  #form-id {
    position: absolute;
    top: -160px;
  }
}
.img-box {
  img {
    display: block;
    width: 100%;
  }
}
.form-box {
  > label {
    color: #646566;
    font-size: 14px;
    padding-left: 17px;
    padding-right: 17px;
    span {
      color: red;
    }
  }
}
.agree-rule {
  display: none;
  font-size: 13px;
  padding-left: 17px;
  margin-top: 20px;
  span {
    color: #68bfc9;
  }
}
/deep/.van-checkbox__label {
  color: #646566;
}
.tips {
  padding-left: 17px;
  padding-right: 17px;
  font-size: 14px;
  font-weight: bold;
  color: #3e95ff;
}
.submit-btn {
  font-weight: bold;
  font-size: 16px;
}
.now-ok {
  position: fixed;
  bottom: 0;
  width: 100%;
  .van-button--normal {
    width: 100%;
    font-size: 16px;
  }
  .van-button--info {
    background-color: rgb(78,145,252);
    border: none;
  }
  .van-icon {
    font-size: 16px;
    vertical-align: sub;
    margin-right: 6px;
  }
  .tips {
    display: none;
    text-align: center;
    color: #646566;
    font-size: 10px;
    font-weight: normal;
    margin-top: 10px;
    margin-bottom: 10px;
    span {
      color: #68bfc9;
    }
  }
}
.img04 {
  margin-bottom: 44px;
}
</style>
